function insertNote(scorecardId,title,note){
    var path="newNote.do?scorecardId="+scorecardId+"&title="+editText(title)+"&note="+editText(note);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													parent.$.fn.colorbox.close();
												});
}



function insertFramework(name,description){
    var path="newFramework.do?name="+editText(name)+"&description="+editText(description);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var textToInsert=   getFrameworkHtml(id,name,description);
													$('.frameworkActive', window.parent.document).attr('src', 'jsp/css/img/icoDesactivadoColor.png');
													$('.frameworkActive', window.parent.document).removeClass('frameworkActive');
													$('#insertBefore', window.parent.document).before(textToInsert);
													parent.$.fn.colorbox.close();
											});
}



function insertColorimetry(name,description,priority,color){
    var path="newColorimetry.do?name="+editText(name)+"&description="+editText(description)+"&priority="+priority+"&color="+color;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){     
													var XMLresponse = buildXMLFromString(data);         
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');   
													var textToInsert = getColorimetryHtml(id, name, color, priority);        
													$('#insertBeforeColorimetry', window.parent.document).before(textToInsert);
													parent.$.fn.colorbox.close();
												});
}



function insertMapDrawing(){
        parent.$.fn.colorbox.close();
}



function editColorimetry(id,name,description,priority,color){
    var path="newColorimetry.do?id="+id+"&name="+editText(name)+"&description="+editText(description)+"&priority="+priority+"&color="+color;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var cell = ($('#edit_colorimetry_'+id,window.parent.document).parent().parent().children('td'));
													$(cell.get(0)).html(name)
													$(cell.get(1)).html(priority)
													var txt = '<span class="squareColor centerImage" id="squareColor" style="display:block;width:25px;height:25px;background-color:#'+color+';" />';
													$(cell.get(2)).html(txt)
													parent.$.fn.colorbox.close();
												});
}





function selectColorimetry(id){
    var path="selectColorimetry.do?colorimetryId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var color = XMLresponse.getElementsByTagName('color')[0].getAttribute('value');
													var description = XMLresponse.getElementsByTagName('description')[0].getAttribute('value');
													var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
													var priority = XMLresponse.getElementsByTagName('priority')[0].getAttribute('value');
													$('#name').attr('value',name);
													$('#description').attr('value',description);
													$('#priority').attr('value',priority);
													$('#squareColor').css('background-color','#'+color);
													$('#color').attr('value',color);
												});
}



function selectMapDrawing(id){
    var path="selectMapDrawing.do?mapDrawingId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
													var description = XMLresponse.getElementsByTagName('description')[0].getAttribute('value');
													$('#name').attr('value',name);
													$('#description').attr('value',description);
												});
}


function selectNoteType(id){
    var path="selectNoteType.do?noteTypeId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
													var description = XMLresponse.getElementsByTagName('description')[0].getAttribute('value');
													$('#name').attr('value',name);
													$('#description').attr('value',description);
												});
}


function selectUnit(id){
    var path="selectUnit.do?unitId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
												var XMLresponse = buildXMLFromString(data);
												var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
												var description = XMLresponse.getElementsByTagName('description')[0].getAttribute('value');
												$('#name').attr('value',name);
												$('#description').attr('value',description);
											});
}


function selectComparation(id){
    var path="selectComparation.do?comparationId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
													$('#name').attr('value',name);
												});
}




function selectAlert(id){
    var path="selectAlert.do?unitId="+id;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
												var XMLresponse = buildXMLFromString(data);
												var name = XMLresponse.getElementsByTagName('name')[0].getAttribute('value');
												var description = XMLresponse.getElementsByTagName('description')[0].getAttribute('value');
												var template = XMLresponse.getElementsByTagName('template')[0].getAttribute('value');

												$('#name').attr('value',name);
												$('#description').attr('value',description);
												$('#template').attr('value',template);
										
											});
}







function deleteColorimetry(id){
	var path="deleteColorimetry.do?id=" + getRealId(id);
	var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id).parent().parent());
													row.remove()
													of = true;
												});

}


function deleteMapDrawing(id){
     var path="deleteMapDrawing.do?id="+getRealId(id);
     var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id,window.parent.document).parent().parent());
													row.remove()
												});
}


function deleteUnit(id){
     var path="deleteUnit.do?id="+getRealId(id);
     var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id,window.parent.document).parent().parent());
													row.remove()
												});
}


function deleteNoteType(id){
     var path="deleteNoteType.do?id="+getRealId(id);
     var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id,window.parent.document).parent().parent());
													row.remove()
												});
}


function deleteComparation(id){
    var path="deleteComparation.do?id="+getRealId(id);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id,window.parent.document).parent().parent());
													row.remove()
												});

   
}


function deleteSMTP(id){
    var path="deleteSMTP.do?id="+getRealId(id);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var row = ($('#'+id,window.parent.document).parent().parent());
													$('.btSMTP', window.parent.document).html('<img src="jsp/css/img/icoAgregar.png" alt="Add" class="add" href="addSMTP.do"/>');
													row.remove()
													configureColorbox();
												});


}



function getColorimetryHtml(id, name, color,priority){

    var htmlColorimetry =   '<tr>'+
                                '<td style="width: 330px;">'+
                                    name+
                                '</td>'+
                                '<td style="width: 200px;">'+
                                    priority+
                                '</td>'+
                                '<td style="width: 200px;"> '+
                                    '<span class="squareColor centerImage" id="squareColor" style="display:block;width:25px;height:25px;background-color:#'+color+';" />'+
                                '</td>'+
                                '<td style="width: 70px;">'+
                                    '<img class="edit editColorimetry centerImage cboxElement" href="addColorimetry.do" src="jsp/css/img/icoEditarColor.png"  alt="See" name="edit" id="edit_colorimetry_'+id+'"  />'+
                                '</td>'+
								/*
                                '<td>'+
                                    '<img class="deleteColorimetry centerImage" href="deleteColorimetry.do" src="jsp/css/img/icoEliminarColor.png" alt="Delete" name="delete" id="delete_colorimetry_'+id+'"  />'+
                                '</td>'+*/
                            '</tr>';
                     
    return htmlColorimetry;


}



function getUnitHtml(id,name,description){

    html =  '<tr>'+
                '<td style="width: 330px;">'+
                    name+
                '</td>'+
                '<td style="width: 400px;">'+
                    description+
                '</td>'+
                '<td style="width: 70px;">' +
                    '<img id="edit_unit_'+id+'" name="edit" alt="See" src="jsp/css/img/icoEditarColor.png" href="addUnit.do?type=edit" class="edit editUnit centerImage cboxElement">'+
                '</td>'+/*
                '<td>'+
                    '<img id="delete_unit_'+id+'" name="delete" alt="Delete" src="jsp/css/img/icoEliminarColor.png" href="deleteUnit.do" class="deleteUnit centerImage">' +
                '</td>'+*/
            '</tr>';


return html;
}



function getSMTPHtml(id,name,description,smtp,mail,password){
    html =  '<tr>' +
                '<td style="width: 190px;">' +
                    name +
                '</td>' +
                '<td style="width: 200px;">'+
                    description+
                '</td>'+
                '<td style="width: 100px;">'+
                    smtp +
                '</td>'+
                '<td style="width: 100px;">'+
                    mail+
                '</td>'+
                '<td style="width: 100px;">'+
                    password+
                '</td>'+
                '<td style="width: 70px;">'+
                    '<img id="edit_smtp_'+id+'" name="edit" alt="See" src="jsp/css/img/icoEditarColor.png" href="addSMTP.do?type=edit" class="edit editSMTP centerImage">'+
                '</td>'+/*
                '<td>'+
                    '<img id="delete_smtp_'+id+'" name="delete" alt="Delete" src="jsp/css/img/icoEliminarColor.png" href="deleteSMTP.do" class="deleteSMTP centerImage">'+
                '</td>'+*/
            '</tr>';

        return html;
}




function editMapDrawing(){
	parent.$.fn.colorbox.close();
}



function editNoteType(id,name,description){
	var path="newNoteType.do?id="+id+"&name="+editText(name)+"&description="+editText(description);
	var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
												var XMLresponse = buildXMLFromString(data);
												var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
												var cell = ($('#edit_note_'+id,window.parent.document).parent().parent().children('td'));
												
												$(cell.get(0)).html(name)
												$(cell.get(1)).html(description)
												
												parent.$.fn.colorbox.close();
												
											});
}


function editUnit(id,name,description){
	var path="newUnit.do?id="+id+"&name="+editText(name)+"&description="+editText(description);
	var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
												var XMLresponse = buildXMLFromString(data);
												var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
												var cell = ($('#edit_unit_'+id,window.parent.document).parent().parent().children('td'));
												
												$(cell.get(0)).html(name)
												$(cell.get(1)).html(description)
										
												parent.$.fn.colorbox.close();
											});
}



function editFramework(id,name,description,cbFramework){


    var path="newFramework.do?id="+id+"&name="+editText(name)+"&description="+editText(description)+"&cbFramework="+cbFramework;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){

												var XMLresponse = buildXMLFromString(data);
												var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
												var cell = ($('#'+id,window.parent.document).parent().parent().children('td'));
										
											  
												var htmlEF = '<a href="acordeon.do?frameworkId='+id+'">'+ name + '</a>';
												var imgSelected = '<img alt="Add" src="jsp/css/img/icoActivadoColor.png" class="centerImage frameworkActive">';
												
												$('.frameworkActive', window.parent.document).attr('src', 'jsp/css/img/icoDesactivadoColor.png');
												$('.frameworkActive', window.parent.document).removeClass('frameworkActive');
										
												$(cell.get(0)).html(htmlEF)
												$(cell.get(1)).html(description)
												$(cell.get(2)).html(imgSelected)
										
												parent.$.fn.colorbox.close();
										
											});
}


function editSMTP(id,name,description,smtp,mail,password){


    var path="newSMTP.do?id="+id+"&name="+editText(name)+"&description="+editText(description)+"&smtp="+editText(smtp)+"&mail="+editText(mail)+"&password="+password;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){

													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var cell = ($('#edit_smtp_'+id,window.parent.document).parent().parent().children('td'));
											
													$(cell.get(0)).html(name)
													$(cell.get(1)).html(description)
													$(cell.get(2)).html(smtp)
													$(cell.get(3)).html(mail)
													$(cell.get(4)).html(password)
											
													parent.$.fn.colorbox.close();
											
												});
}

function insertUnit(name,description){
    var path="newUnit.do?name="+editText(name)+"&description="+editText(description);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
												var XMLresponse = buildXMLFromString(data);
												var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
												var textToInsert =getUnitHtml(id,name,description);
												
												$('#insertBeforeUnit', window.parent.document).before(textToInsert);
												parent.$.fn.colorbox.close();
											});
}


function editComparation(id,name){

    var path="newComparation.do?id="+id+"&name="+editText(name);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){										
												var XMLresponse = buildXMLFromString(data);
												var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
												var cell = ($('#edit_comparation_'+id,window.parent.document).parent().parent().children('td'));
										
												$(cell.get(0)).html(name)
												parent.$.fn.colorbox.close();
											});
}



function insertComparation(name,description){

    var path="newComparation.do?name="+editText(name)+"&description="+editText(description);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var textToInsert = getComparationHtml(id,name);
													$('#insertBeforeComparation', window.parent.document).before(textToInsert);
													parent.$.fn.colorbox.close();
												});
}







function insertNoteType(name,description){

    var path="newNoteType.do?name="+editText(name)+"&description="+editText(description);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var textToInsert = getNoteTypeHtml(id, name, description);

													$('#insertBeforeNoteType', window.parent.document).before(textToInsert);
													parent.$.fn.colorbox.close();
												});
}




function insertSmtp(name,description,smtp,mail,password){



    var path="newSMTP.do?name="+editText(name)+"&description="+editText(description)+"&smtp="+editText(smtp)+"&mail="+editText(mail)+"&password="+editText(password);
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var textToInsert = getSMTPHtml(id,name,description,smtp,mail,password);
												
													$('#insertBeforeSMTP', window.parent.document).before(textToInsert);
													$('.btSMTP', window.parent.document).empty();
													parent.$.fn.colorbox.close();
												});
}

function insertTopic(topic,description, responsable, expectedTime, type){

    var path="newTopic.do?topic="+editText(topic)+"&description="+editText(description)+"&responsable="+editText(responsable)+"&expectedTime="+expectedTime+"&type="+type;
    var ajax = new accesoAJAX.CargadorDeDatos("POST",path,function(data){
													var XMLresponse = buildXMLFromString(data);
													var id = XMLresponse.getElementsByTagName('id')[0].getAttribute('value');
													var textToInsert =getUnitHtml(id,name,description);

													$('#insertBeforeUnit', window.parent.document).before(textToInsert);
													parent.$.fn.colorbox.close();
												});
}

function getNoteTypeHtml(id,name,description){
    var html =      '<tr>'+
                        '<td style="width: 330px;">'+
                            name+
                        '</td>'+
                        '<td style="width: 400px;">'+
                            description+
                        '</td>'+
                        '<td style="width: 70px;">'+
                            '<img id="edit_note_'+id+'" name="edit" alt="See" src="jsp/css/img/icoEditarColor.png" href="addNoteType.do?type=edit" class="edit editNoteType centerImage cboxElement">'+
                        '</td>'+/*
                        '<td>'+
                            ' <img id="delete_note_'+id+'" name="delete" alt="Delete" src="jsp/css/img/icoEliminarColor.png" href="deleteNoteType.do" class="deleteNoteType centerImage">'+
                        '</td>'+*/
                    '</tr>';

    return html;
}

function getComparationHtml(id, name){
    var html =  '<tr>'+
                    '<td>'+
                        name+
                    '</td>'+
                    '<td style="width: 70px;">'+
                        '<img id="edit_comparation_'+id+'" name="edit" alt="See" src="jsp/css/img/icoEditarColor.png" href="addComparation.do?type=edit" class="edit editComparator centerImage cboxElement">'+
                    '</td>'+/*
                    '<td style="width: 70px;">'+
                        '<img id="delete_comparation_'+id+'" name="delete" alt="Delete" src="jsp/css/img/icoEliminarColor.png" href="deleteComparation.do" class="deleteComparation centerImage">'+
                    '</td>'+*/
                '</tr>';

    return html;
}

function getFrameworkHtml(id,name,description){
	html = '<tr>'+
			' <td style="width: 250px;">'+
				'<a href="acordeon.do?frameworkId='+id+'">'+name+
				'</a>'+
			'</td>'+
			'<td style="width: 480px;">'+
				description+
			'</td>'+
			' <td style="width: 100px;">'+
				' <img alt="Add" src="jsp/css/img/icoActivadoColor.png" class="centerImage frameworkActive">'+
			'</td>'+
			'<td>'+
				'<img class="edit editFramework centerImage cboxElement" href="addFramework.do?type=edit" src="jsp/css/img/icoEditarColor.png" alt="edit" name="edit" id="'+id+'" />'+
		 '</td>'+
		'</tr>';

	return html;
}


function closeLiveWindow(){
    parent.$.fn.colorbox.close();
}

function closeLiveWindowMap(){
	parent.location.reload();
	parent.$.fn.colorbox.close();
}

function getRealId(compleateId){
    ida = compleateId.split('_');
    realId = ida[ida.length -1];
	
    return realId;
}

function configureColorbox(){

	//Cleaning all previous clicks
	//(If we don´t do it there will be a space-time paradox that will destroy the world as we know)
	//Ok, that wasn´t true but the events get duplicated
	$(".deleteColorimetry, .deleteMapDrawing, .deleteNoteType, .deleteUnit, .deleteComparation, .edit").unbind('click');

	//$(".add").colorbox({width:"770px", height:"398px", iframe:true,
        $(".add").colorbox({width:"720px", height:"480px", iframe:true,
		onClosed:function(){
			configureColorbox();
		}
	});

	//$(".edit").colorbox({width:"770px", height:"398px", iframe:true,
        $(".edit").colorbox({width:"720px", height:"480px", iframe:true,
		onClosed:function(){
			configureColorbox();
		}
	});

	$(".deleteColorimetry").click( function(){
		deleteColorimetry($(this).attr('id'));
		$(this).attr('href', 'deleteColorimetry.do?id='+ $(this).attr('id'));
	});

	$(".deleteMapDrawing").click( function(){
		deleteMapDrawing($(this).attr('id'));
		$(this).attr('href', 'deleteMapDrawing.do?id='+ $(this).attr('id'));
	});

	$(".deleteNoteType").click( function(){
		deleteNoteType($(this).attr('id'));
		$(this).attr('href', 'deleteNoteType.do?id='+ $(this).attr('id'));
	});

	$(".deleteUnit").click( function(){
		deleteUnit($(this).attr('id'));
		$(this).attr('href', 'deleteUnit.do?id='+ $(this).attr('id'));
	});

	$(".deleteComparation").click( function(){
		deleteComparation($(this).attr('id'));
		$(this).attr('href', 'deleteComparation.do?id='+ $(this).attr('id'));
	});

	$(".deleteSMTP").click( function(){
		deleteSMTP($(this).attr('id'));
		$(this).attr('href', 'deleteSMTP.do?id='+ $(this).attr('id'));
	});

	$(".edit").click(function(){
		$(this).unbind('click');
		$(this).attr('href',
		$(this).attr('href').split('?')[0]+'?id='+ getRealId($(this).attr('id'))+'&type=edit');
	});

	$(".btLeftbar").click(function(){
		$(".active").removeClass('active');
		$(this).addClass('active');
	});


}